package blt.app_core.utils;

/**
 * Description :
 * <p>
 * Creation    : 2016/12/8
 * Author      : xugaopan
 */

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Nullable;

public class CommonPref {


    private static final CommonPref sInstance = new CommonPref();

    private SharedPreferences mPreferences;

    public static CommonPref getInstance() {
        return sInstance;
    }

    public void init(Context context, String prefName) {
        mPreferences = context.getSharedPreferences(prefName, Context.MODE_PRIVATE);
    }

    public String getString(String key, @Nullable String defValue) {
        if (mPreferences != null) {
            return mPreferences.getString(key, defValue);
        } else {
            throw new IllegalStateException("Please call init() to init SharedPreferences!");
        }
    }

    public int getInt(String key, @Nullable int defValue) {
        if (mPreferences != null) {
            return mPreferences.getInt(key, defValue);
        } else {
            throw new IllegalStateException("Please call init() to init SharedPreferences!");
        }
    }

    public long getLong(String key, @Nullable long defValue) {
        if (mPreferences != null) {
            return mPreferences.getLong(key, defValue);
        } else {
            throw new IllegalStateException("Please call init() to init SharedPreferences!");
        }
    }

    public boolean getBoolean(String key, @Nullable boolean defValue) {
        if (mPreferences != null) {
            return mPreferences.getBoolean(key, defValue);
        } else {
            throw new IllegalStateException("Please call init() to init SharedPreferences!");
        }
    }

    public void saveString(String key, String value) {
        if (mPreferences != null) {
            SharedPreferences.Editor editor = mPreferences.edit();
            editor.putString(key, value);
            editor.apply();
        } else {
            throw new IllegalStateException("Please call init() to init SharedPreferences!");
        }
    }

    public void saveInt(String key, int value) {
        if (mPreferences != null) {
            SharedPreferences.Editor editor = mPreferences.edit();
            editor.putInt(key, value);
            editor.apply();
        } else {
            throw new IllegalStateException("Please call init() to init SharedPreferences!");
        }
    }

    public void saveLong(String key, Long value) {
        if (mPreferences != null) {
            SharedPreferences.Editor editor = mPreferences.edit();
            editor.putLong(key, value);
            editor.apply();
        } else {
            throw new IllegalStateException("Please call init() to init SharedPreferences!");
        }
    }

    public void saveBoolean(String key, boolean value) {
        if (mPreferences != null) {
            SharedPreferences.Editor editor = mPreferences.edit();
            editor.putBoolean(key, value);
            editor.apply();
        } else {
            throw new IllegalStateException("Please call init() to init SharedPreferences!");
        }
    }

    public void removeForKey(String key) {
        if (mPreferences != null) {
            SharedPreferences.Editor editor = mPreferences.edit();
            editor.remove(key);
            editor.apply();
        } else {
            throw new IllegalStateException("Please call init() to init SharedPreferences!");
        }
    }

    public void clear() {
        mPreferences.edit().clear().apply();
    }



    public SharedPreferences.Editor getEditor() {
        if (mPreferences != null) {
            SharedPreferences.Editor editor = mPreferences.edit();
            return editor;
        } else {
            throw new IllegalStateException("Please call init() to init SharedPreferences!");
        }
    }
}
